package com.ctbiyi.ureport.component;

import com.bstek.ureport.definition.datasource.BuildinDatasource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

/**
 * @projectName: ureport-demo
 * @packageName: com.ctbiyi.ureport.compnent
 * @description:实例化一个内置数据源
 * @author: hy.bai
 * @date: 2023-05-13 19:32
 */
@Component
public class UReportDataSource implements BuildinDatasource {

    @Autowired
    private DataSource dataSource;

    /**
     * @return 返回数据源名称
     */
    @Override
    public String name() {
        return "CurrentDataSource";
    }

    /**
     * @return 返回当前采用数据源的一个连接
     */
    @Override
    public Connection getConnection() {
        try {
            return dataSource.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
